﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NHibernate;
using NHibernate.Transform;
using ZLCMS.Data.Model;
using ZLCMS.Business;
using DotNet.Common.NHibernateUtil.QueryFilter;
using DotNet.Common.NHibernateUtil;

namespace ZLCMS.Web.Example.Doc
{
    public partial class UseFilter : System.Web.UI.Page
    {
        protected string _c;

        protected void Page_Load(object sender, EventArgs e)
        {
            int currentPageIndex = string.IsNullOrEmpty(Request[anp_list.UrlPageIndexName]) ? 1 : int.Parse(Request[anp_list.UrlPageIndexName]);
            anp_list.PageSize = string.IsNullOrEmpty(Request["pagen"]) ? 15 : int.Parse(Request["pagen"]);

            BindAgentList(currentPageIndex, anp_list.PageSize);
        }
        
        /// <summary>
        /// 绑定代理人数据
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">记录条数</param>
        private void BindAgentList(int pageIndex, int pageSize)
        {
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                QueryFilterList filters = new QueryFilterList();

                string fromSql = " from DocInfo p, TreeDef t where p.TypeId = t.Id ";

                if (_c != null)
                {
                    filters.Add(new StringFilter("t.Prefix", "Prefix", _c, StringCompairType.Like));
                }

                string condtion = "";

                foreach (DataFilter filter in filters)
                {
                    condtion += " and " + filter.GetSection();
                }

                //获取记录总数
                IQuery cntquery = session.CreateQuery("select count(*)" + fromSql + condtion);
                filters.SetUp(cntquery);

                anp_list.RecordCount = Convert.ToInt32(cntquery.UniqueResult());

                string select = " select p.Id as Id ";

                //获取数据
                IQuery dataquery = session.CreateQuery(select + fromSql + condtion);
                filters.SetPager(dataquery, pageIndex, pageSize);
                filters.SetUp(dataquery);

                rp_product.DataSource = dataquery.SetResultTransformer(Transformers.AliasToEntityMap).List();
                rp_product.DataBind();
            }
        }
    }
}